<template>
	<view class="main-container">
		<!-- 根据当前选中的tab条件渲染内容 -->
		<view class="content-container">
			<!-- 首页 -->
			<view v-if="currentTab == 0" class="page-container">
				<home-page @clickSqEvent="switchTab"></home-page>
			</view>
			
			<!-- 广场 -->
			<view v-if="currentTab == 1" class="page-container">
				<neighobr></neighobr>
			</view>
			
			<!-- 商城 -->
			<view v-if="currentTab == 2" class="page-container">
				<shop-page></shop-page>
			</view>
			
			<!-- 社区 -->
			<view v-if="currentTab == 3" class="page-container">
				<community-page ref="community" :activeId="activeId"></community-page>
			</view>
			
			<!-- 我的 -->
			<view v-if="currentTab == 4" class="page-container">
				<user-page></user-page>
			</view>
		</view>
		
		<!-- 自定义tabbar -->
		<view class="custom-tabbar between">
			<view 
				v-for="(item, index) in tabList" 
				:key="item.url" 
				class="tabbar-item" 
				:class="{active: currentTab == item.url}"
				@click="switchTab(item)"
			>
				<view class="tabbar-icon">
					<i class="font48 iconfont" :class="currentTab == item.url ? item.icon + '-check' : item.icon"></i>
				</view>
				<text class="tabbar-text font24">{{item.name}}</text>
			</view>
		</view>
		<version></version>
		<coupon-pop :show="couponShow" @close="handleCouponClose"></coupon-pop>
	</view>
</template>

<script>
	import { imgUrl } from '../utils/utils.js';
	
	// 导入各个页面组件
	import HomePage from './index/index.vue';
	import ShopPage from './shop/shop.vue';
	import CommunityPage from './community/community.vue';
	import UserPage from './person/person.vue';
	import neighobr from './neighobr/neighobr.vue';
	import version from '@/components/version/index.vue'
	import couponPop from '@/components/coupon/pop.vue'
	// import headerVue from '../components/header.vue';
	
	export default {
		components: {
			HomePage,
			ShopPage,
			CommunityPage,
			UserPage,
			neighobr,
			version,
			couponPop
		},
		data() {
			return {
				imgUrl,
				currentTab: 0,
				tabList: [
					{ name: '首页', icon: 'icon-home', url: 0 },
					{ name: '邻工', icon: 'icon-lingong', url: 1 },
					{ name: '商城', icon: 'icon-shop', url: 2 },
					{ name: '社区', icon: 'icon-shequ', url: 3 },
					{ name: '我的', icon: 'icon-mine', url: 4 }
				],
				activeId:0,
				couponShow: this.$store.state.couponShow, // 首次进入显示，之后不再显示
			}
		},
		onLoad(option){
			this.currentTab = option.url || 0
		},
		methods: {
			switchTab(e) {
				if (this.currentTab === e.url) return;
				uni.redirectTo({url:`/pages/main?url=${e.url}`})
				// 判断是否有tab参数
				if (e.url == 3 && typeof e.tab !== 'undefined') {
					this.activeId = e.tab
				}
				
				// 添加动画效果
				setTimeout(() => {
					this.currentTab = e.url;
				}, 50);
			},
			
			// 显示优惠券弹窗
			showCouponPopup() {
				this.couponShow = true
				this.$store.commit('SET_COUPON_SHOW', true)
			},
			
			// 优惠券弹窗关闭事件
			handleCouponClose() {
				this.couponShow = false
				this.$store.commit('SET_COUPON_SHOW', false)
			},
		},
	}
</script>

<style lang="scss" scoped>
	.main-container {
		display: flex;
		flex-direction: column;
		height: 100vh;
	}
	
	.content-container {
		flex: 1;
		overflow: hidden;
		position: relative;
	}
	
	.page-container {
		width: 100%;
		height: 100%;
		overflow: auto;
	}
	
	.message-page {
		display: flex;
		flex-direction: column;
		height: 100%;
	}
	
	.message-content {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.message-placeholder {
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	
	.message-icon {
		width: 200rpx;
		height: 200rpx;
		margin-bottom: 30rpx;
	}
	
	.message-text {
		color: $uni-text-color-grey;
	}
	
	.custom-tabbar {
		display: flex;
		// min-height: 100rpx;
		background: #FFFFFF;
		padding: 18rpx 30rpx env(safe-area-inset-bottom);
		box-shadow: 0rpx -4rpx 25rpx 0rpx rgba(0,0,0,0.03);
		z-index: 100;
		
		.tabbar-item {
			//flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			color: $uni-text-color-grey;
			transition: all 0.3s;
			position: relative;
			padding: 0 20rpx;
			
			// &::after {
			// 	content: '';
			// 	position: absolute;
			// 	bottom: -6rpx;
			// 	left: 50%;
			// 	transform: translateX(-50%) scale(0);
			// 	width: 16rpx;
			// 	height: 6rpx;
			// 	border-radius: 3rpx;
			// 	background-color: $uni-text-color;
			// 	opacity: 0;
			// 	transition: all 0.3s ease;
			// }
			
			&:nth-child(1) {
				padding-left: 0;
			}
			
			&:nth-child(5) {
				padding-right: 0;
			}
			
			.tabbar-icon {
				height: 44rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-bottom: 6rpx;
				
				i {
					font-size: 44rpx;
					transition: transform 0.2s ease;
				}
				
				.shequ-icon {
					width: 100rpx;
					height: 100rpx;
					margin-top: -38rpx;
				}
			}
			
			&:active {
				i {
					transform: scale(0.9);
				}
			}
		}
		
		.active {
			color: $uni-color-primary;
			
			&::after {
				opacity: 1;
				transform: translateX(-50%) scale(1);
			}
			
			i {
				font-weight: 400;
				// transform: scale(1.05);
			}
		}
	}
	
	.test-btn {
		position: fixed;
		top: 100rpx;
		right: 30rpx;
		background: #007AFF;
		color: #FFFFFF;
		padding: 20rpx 30rpx;
		border-radius: 50rpx;
		font-size: 24rpx;
		z-index: 999;
		box-shadow: 0 4rpx 16rpx rgba(0, 122, 255, 0.3);
		
		&:active {
			transform: scale(0.95);
		}
	}
</style> 